package com.hn658.base.message.account.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hn658.base.message.account.dao.db.IAccountManagerDBDAO;
import com.hn658.base.message.account.dao.redis.IAccountManagerRedisDAO;
import com.hn658.base.message.account.entity.AccountUserInfoEO;
import com.hn658.base.message.account.service.IAccountUserManagerService;
import com.hn658.base.message.common.contants.MessageContants;
import com.hn658.framework.common.AbstractBusinessObject;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.framework.shared.utils.UUIDUtils;

@Component
public class AccountUserManagerServiceImpl extends AbstractBusinessObject implements IAccountUserManagerService{
	
	@Autowired
	private IAccountManagerDBDAO userManagerDBDAO;
	
	@Autowired
	private IAccountManagerRedisDAO userManagerRedisDAO;
	
	@Override
	@Transactional
	public List<AccountUserInfoEO> findUserList() {
		List<AccountUserInfoEO> findUserList = userManagerDBDAO.findUserList();
		return findUserList;
	}
	
	@Override
	@Transactional
	public void addUser(AccountUserInfoEO userInfoEO) {
		if(userInfoEO==null){
			throw new BusinessException("不能为空");
		}
		userManagerDBDAO.insert(userInfoEO);
		userManagerRedisDAO.save(userInfoEO);
	}
	
	@Override
	@Transactional
	public void updateAccount(AccountUserInfoEO userInfoEO) {
		if(userInfoEO==null){
			throw new BusinessException("不能为空");
		}
		userManagerDBDAO.update(userInfoEO);
		userInfoEO=userManagerDBDAO.findByid(userInfoEO.getId());
		userManagerRedisDAO.save(userInfoEO);
	}
	
	@Override
	@Transactional
	public AccountUserInfoEO disableUser(Long id){
		if(id==null){
			throw new BusinessException("id不能为空");
		}
		AccountUserInfoEO userInfoEO=userManagerDBDAO.findByid(id);
		if(userInfoEO==null){
			throw new BusinessException("用户不存在");
		}
		if(userInfoEO.getIsDeleted()){
			throw new BusinessException("此用户已经被禁用");
		}
		userInfoEO.setIsDeleted(MessageContants.YES);
		userManagerDBDAO.update(userInfoEO);
		userManagerRedisDAO.save(userInfoEO);
		
		return userInfoEO;
	}
	
	@Override
	@Transactional
	public AccountUserInfoEO enableUser(Long id) {
		if(id==null){
			throw new BusinessException("id不能为空");
		}
		AccountUserInfoEO userInfoEO=userManagerDBDAO.findByid(id);
		if(userInfoEO==null){
			throw new BusinessException("用户不存在");
		}
		if(userInfoEO.getIsDeleted()==MessageContants.NO){
			throw new BusinessException("此用户已经被启用");
		}
		userInfoEO.setIsDeleted(MessageContants.NO);
		userManagerDBDAO.update(userInfoEO);
		userManagerRedisDAO.save(userInfoEO);
		return userInfoEO;
	}
	
	@Override
	public GenericPage<AccountUserInfoEO> findByAccountUser(Map<String,Object> map,int limit, int start, String sortBy, boolean isAsc) {
		if(StringUtils.isEmpty(sortBy)){
			sortBy = "ID";
		}
		return userManagerDBDAO.selectByMap(map, limit, start, sortBy, isAsc);
	}
	
	@Override
	public AccountUserInfoEO findAccountByAppIdUid(String appKey, String userId) {
		AccountUserInfoEO accountInfo = userManagerRedisDAO.getAccount(appKey, userId);
		if(accountInfo==null){
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("userId", userId);
			map.put("appKey", appKey);
			map.put("isDeleted", MessageContants.NO);
			List<AccountUserInfoEO> list = userManagerDBDAO.selectByMap(map);
			if(list!=null&&list.size()>0){
				accountInfo= list.get(0);
			}
		}
		return accountInfo;
	}
	@Override
	public String createToken(String appKey, String userId, String name,
			String portraitUri) {
		//判断是否存在用户，如果不存在，生成并保存用户
        AccountUserInfoEO accountUserInfo =	this.findAccountByAppIdUid(appKey, userId);
        if(accountUserInfo==null){
        	accountUserInfo = new AccountUserInfoEO();
        	accountUserInfo.setAppKey(appKey);
        	accountUserInfo.setUserId(userId);
        	accountUserInfo.setNickName(name);
        	accountUserInfo.setAvatarURL(portraitUri);
        	accountUserInfo.setCreateTime(new Date());
        	accountUserInfo.setLastUpdateTime(new Date());
        	accountUserInfo.setIsDeleted(MessageContants.NO);
        	this.addUser(accountUserInfo);
        }
		// 生成新的token
        String token = UUIDUtils.getUUID();
        // 保存token
        userManagerRedisDAO.setAccessToken(appKey,userId, token);
        return token;
	}

	@Override
	public String findUserToken(String appKey, String userId) {
		return userManagerRedisDAO.getAccessToken(appKey, userId);
	}

	@Override
	public List<String> findAccountId(String appKey) {
		if(StringUtils.isBlank(appKey)){
			throw new BusinessException("应用id不能为空");
		}
		return userManagerDBDAO.findAccountId(appKey);
	}

	@Override
	public List<AccountUserInfoEO> queryAccountListByUid(String appKey,
			List<String> ids) {
		if(appKey==null||ids==null||ids.size()<=0){
			throw new BusinessException("应用id不能为空,用户id列表不能为空");
		}
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("appKey", appKey);
		queryMap.put("userIds", ids);
		queryMap.put("isDeleted", MessageContants.NO);
		return userManagerDBDAO.selectByMap(queryMap,"last_update_time",false);
	}

	@Override
	public List<AccountUserInfoEO> queryAccountListByLastTime(String appKey,
			Date time) {
		if(appKey==null||time==null){
			throw new BusinessException("应用id不能为空,或最后更新日期不能为空");
		}
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("appKey", appKey);
		queryMap.put("endStartTime", time);
		queryMap.put("isDeleted", MessageContants.NO);
		return userManagerDBDAO.selectByMap(queryMap,"last_update_time",false);
	}
	
	
}
